parameters:
  - name: VSCODE_QUALITY
    type: string
  - name: VSCODE_ARCH
    type: string
  - name: VSCODE_CIBUILD
    type: boolean
  - name: VSCODE_RUN_UNIT_TESTS
    type: boolean
  - name: VSCODE_RUN_INTEGRATION_TESTS
    type: boolean
  - name: VSCODE_RUN_SMOKE_TESTS
    type: boolean

steps:
  - ${{ if eq(parameters.VSCODE_QUALITY, 'oss') }}:
    - checkout: self
      fetchDepth: 1
      retryCountOnTaskFailure: 3

  - task: NodeTool@0
    inputs:
      versionSource: fromFile
      versionFilePath: .nvmrc
      nodejsMirror: https://github.com/joaomoreno/node-mirror/releases/download

  - task: UsePythonVersion@0
    inputs:
      versionSpec: "3.x"
      addToPath: true

  - ${{ if ne(parameters.VSCODE_QUALITY, 'oss') }}:
    - template: ../distro/download-distro.yml@self

    - task: AzureKeyVault@1
      displayName: "Azure Key Vault: Get Secrets"
      inputs:
        azureSubscription: "vscode-builds-subscription"
        KeyVaultName: vscode-build-secrets
        SecretsFilter: "github-distro-mixin-password,ESRP-PKI,esrp-aad-username,esrp-aad-password"

    - task: DownloadPipelineArtifact@2
      inputs:
        artifact: Compilation
        path: $(Build.ArtifactStagingDirectory)
      displayName: Download compilation output

    - task: ExtractFiles@1
      displayName: Extract compilation output
      inputs:
        archiveFilePatterns: "$(Build.ArtifactStagingDirectory)/compilation.tar.gz"
        cleanDestinationFolder: false

  - powershell: node build/setup-npm-registry.js $env:NPM_REGISTRY
    condition: and(succeeded(), ne(variables['NPM_REGISTRY'], 'none'))
    displayName: Setup NPM Registry

  - pwsh: |
      mkdir .build -ea 0
      node build/azure-pipelines/common/computeNodeModulesCacheKey.js win32 $(VSCODE_ARCH) > .build/yarnlockhash
    displayName: Prepare node_modules cache key

  - task: Cache@2
    inputs:
      key: '"node_modules" | .build/yarnlockhash'
      path: .build/node_modules_cache
      cacheHitVar: NODE_MODULES_RESTORED
    displayName: Restore node_modules cache

  - powershell: 7z.exe x .build/node_modules_cache/cache.7z -aoa
    condition: and(succeeded(), eq(variables.NODE_MODULES_RESTORED, 'true'))
    displayName: Extract node_modules cache

  - powershell: |
      . build/azure-pipelines/win32/exec.ps1
      $ErrorActionPreference = "Stop"
      exec { npm config set registry "$env:NPM_REGISTRY" --location=project }
      # npm >v7 deprecated the `always-auth` config option, refs npm/cli@72a7eeb
      # following is a workaround for yarn to send authorization header
      # for GET requests to the registry.
      exec { Add-Content -Path .npmrc -Value "always-auth=true" }
      exec { yarn config set registry "$env:NPM_REGISTRY" }
    condition: and(succeeded(), ne(variables.NODE_MODULES_RESTORED, 'true'), ne(variables['NPM_REGISTRY'], 'none'))
    displayName: Setup NPM & Yarn

  - task: npmAuthenticate@0
    inputs:
      workingFile: .npmrc
    condition: and(succeeded(), ne(variables.NODE_MODULES_RESTORED, 'true'), ne(variables['NPM_REGISTRY'], 'none'))
    displayName: Setup NPM Authentication

  - powershell: |
      . build/azure-pipelines/win32/exec.ps1
      . build/azure-pipelines/win32/retry.ps1
      $ErrorActionPreference = "Stop"
      retry { exec { yarn --frozen-lockfile --check-files } }
    env:
      npm_config_arch: $(VSCODE_ARCH)
      CHILD_CONCURRENCY: 1
      ELECTRON_SKIP_BINARY_DOWNLOAD: 1
      PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
      GITHUB_TOKEN: "$(github-distro-mixin-password)"
    displayName: Install dependencies
    condition: and(succeeded(), ne(variables.NODE_MODULES_RESTORED, 'true'))

  - ${{ if ne(parameters.VSCODE_QUALITY, 'oss') }}:
    - powershell: node build/azure-pipelines/distro/mixin-npm
      condition: and(succeeded(), ne(variables.NODE_MODULES_RESTORED, 'true'))
      displayName: Mixin distro node modules

  - powershell: |
      . build/azure-pipelines/win32/exec.ps1
      $ErrorActionPreference = "Stop"
      exec { node build/azure-pipelines/common/listNodeModules.js .build/node_modules_list.txt }
      exec { mkdir -Force .build/node_modules_cache }
      exec { 7z.exe a .build/node_modules_cache/cache.7z -mx3 `@.build/node_modules_list.txt }
    condition: and(succeeded(), ne(variables.NODE_MODULES_RESTORED, 'true'))
    displayName: Create node_modules archive

  - ${{ if ne(parameters.VSCODE_QUALITY, 'oss') }}:
    - powershell: node build/azure-pipelines/distro/mixin-quality
      displayName: Mixin distro quality

  - template: ../common/install-builtin-extensions.yml@self

  - ${{ if and(ne(parameters.VSCODE_CIBUILD, true), ne(parameters.VSCODE_QUALITY, 'oss')) }}:
    - powershell: node build\lib\policies
      displayName: Generate Group Policy definitions
      retryCountOnTaskFailure: 3

  - ${{ if eq(parameters.VSCODE_QUALITY, 'oss') }}:
    - powershell: yarn gulp "transpile-client-swc" "transpile-extensions"
      env:
        GITHUB_TOKEN: "$(github-distro-mixin-password)"
      displayName: Transpile client and extensions

  - ${{ else }}:
    - ${{ if and(ne(parameters.VSCODE_CIBUILD, true), eq(parameters.VSCODE_QUALITY, 'insider')) }}:
      - powershell: node build/win32/explorer-appx-fetcher .build/win32/appx
        displayName: Download Explorer Sparse Package

    - powershell: |
        . build/azure-pipelines/win32/exec.ps1
        $ErrorActionPreference = "Stop"
        exec { yarn gulp "vscode-win32-$(VSCODE_ARCH)-min-ci" }
        exec { yarn gulp "vscode-win32-$(VSCODE_ARCH)-inno-updater" }
        echo "##vso[task.setvariable variable=BUILT_CLIENT]true"
        echo "##vso[task.setvariable variable=CodeSigningFolderPath]$(agent.builddirectory)/VSCode-win32-$(VSCODE_ARCH)"
      env:
        GITHUB_TOKEN: "$(github-distro-mixin-password)"
      displayName: Build client

    - powershell: |
        . build/azure-pipelines/win32/exec.ps1
        $ErrorActionPreference = "Stop"
        exec { yarn gulp "vscode-reh-win32-$(VSCODE_ARCH)-min-ci" }
        mv ..\vscode-reh-win32-$(VSCODE_ARCH) ..\vscode-server-win32-$(VSCODE_ARCH) # TODO@joaomoreno
        echo "##vso[task.setvariable variable=BUILT_SERVER]true"
        echo "##vso[task.setvariable variable=CodeSigningFolderPath]$(CodeSigningFolderPath),$(agent.builddirectory)/vscode-server-win32-$(VSCODE_ARCH)"
      env:
        GITHUB_TOKEN: "$(github-distro-mixin-password)"
      displayName: Build server
      condition: and(succeeded(), ne(variables['VSCODE_ARCH'], 'arm64'))

    - powershell: |
        . build/azure-pipelines/win32/exec.ps1
        $ErrorActionPreference = "Stop"
        exec { yarn gulp "vscode-reh-web-win32-$(VSCODE_ARCH)-min-ci" }
        mv ..\vscode-reh-web-win32-$(VSCODE_ARCH) ..\vscode-server-win32-$(VSCODE_ARCH)-web # TODO@joaomoreno
        echo "##vso[task.setvariable variable=BUILT_WEB]true"
      env:
        GITHUB_TOKEN: "$(github-distro-mixin-password)"
      displayName: Build server (web)
      condition: and(succeeded(), ne(variables['VSCODE_ARCH'], 'arm64'))

  - ${{ if or(eq(parameters.VSCODE_RUN_UNIT_TESTS, true), eq(parameters.VSCODE_RUN_INTEGRATION_TESTS, true), eq(parameters.VSCODE_RUN_SMOKE_TESTS, true)) }}:
    - template: product-build-win32-test.yml@self
      parameters:
        VSCODE_QUALITY: ${{ parameters.VSCODE_QUALITY }}
        VSCODE_ARCH: ${{ parameters.VSCODE_ARCH }}
        VSCODE_RUN_UNIT_TESTS: ${{ parameters.VSCODE_RUN_UNIT_TESTS }}
        VSCODE_RUN_INTEGRATION_TESTS: ${{ parameters.VSCODE_RUN_INTEGRATION_TESTS }}
        VSCODE_RUN_SMOKE_TESTS: ${{ parameters.VSCODE_RUN_SMOKE_TESTS }}
        ${{ if ne(parameters.VSCODE_QUALITY, 'oss') }}:
          PUBLISH_TASK_NAME: 1ES.PublishPipelineArtifact@1

  - ${{ if ne(parameters.VSCODE_CIBUILD, true) }}:
    - ${{ if ne(parameters.VSCODE_QUALITY, 'oss') }}:
      - task: DownloadPipelineArtifact@2
        inputs:
          artifact: unsigned_vscode_cli_win32_$(VSCODE_ARCH)_cli
          patterns: "**"
          path: $(Build.ArtifactStagingDirectory)/cli
        displayName: Download VS Code CLI

      - powershell: |
          . build/azure-pipelines/win32/exec.ps1
          $ErrorActionPreference = "Stop"
          $ArtifactName = (gci -Path "$(Build.ArtifactStagingDirectory)/cli" | Select-Object -last 1).FullName
          Expand-Archive -Path $ArtifactName -DestinationPath "$(Build.ArtifactStagingDirectory)/cli"
          $AppProductJson = Get-Content -Raw -Path "$(agent.builddirectory)\VSCode-win32-$(VSCODE_ARCH)\resources\app\product.json" | ConvertFrom-Json
          $CliAppName = $AppProductJson.tunnelApplicationName
          $AppName = $AppProductJson.applicationName
          Move-Item -Path "$(Build.ArtifactStagingDirectory)/cli/$AppName.exe" -Destination "$(agent.builddirectory)/VSCode-win32-$(VSCODE_ARCH)/bin/$CliAppName.exe"
        displayName: Move VS Code CLI

      - task: UseDotNet@2
        inputs:
          version: 6.x

      - task: EsrpClientTool@1
        displayName: Download ESRPClient

      - powershell: |
          . build/azure-pipelines/win32/exec.ps1
          $ErrorActionPreference = "Stop"
          $EsrpClientTool = (gci -directory -filter EsrpClientTool_* $(Agent.RootDirectory)\_tasks | Select-Object -last 1).FullName
          $EsrpCliZip = (gci -recurse -filter esrpcli.*.zip $EsrpClientTool | Select-Object -last 1).FullName
          mkdir -p $(Agent.TempDirectory)\esrpcli
          Expand-Archive -Path $EsrpCliZip -DestinationPath $(Agent.TempDirectory)\esrpcli
          $EsrpCliDllPath = (gci -recurse -filter esrpcli.dll $(Agent.TempDirectory)\esrpcli | Select-Object -last 1).FullName
          echo "##vso[task.setvariable variable=EsrpCliDllPath]$EsrpCliDllPath"
        displayName: Find ESRP CLI

      - powershell: node build\azure-pipelines\common\sign $env:EsrpCliDllPath sign-windows $(ESRP-PKI) $(esrp-aad-username) $(esrp-aad-password) $(CodeSigningFolderPath) '*.dll,*.exe,*.node'
        displayName: Codesign executables and shared libraries

    - ${{ if eq(parameters.VSCODE_QUALITY, 'insider') }}:
      - powershell: node build\azure-pipelines\common\sign $env:EsrpCliDllPath sign-windows-appx $(ESRP-PKI) $(esrp-aad-username) $(esrp-aad-password) $(CodeSigningFolderPath) '*.appx'
        displayName: Codesign context menu appx package

    - ${{ if ne(parameters.VSCODE_QUALITY, 'oss') }}:
      - powershell: |
          $PackageJson = Get-Content -Raw -Path ..\VSCode-win32-$(VSCODE_ARCH)\resources\app\package.json | ConvertFrom-Json
          $Version = $PackageJson.version
          echo "##vso[task.setvariable variable=VSCODE_VERSION]$Version"
        condition: succeededOrFailed()
        displayName: Get product version

      - powershell:  |
          . build/azure-pipelines/win32/exec.ps1
          $ErrorActionPreference = "Stop"
          $ArchivePath = ".build\win32-$(VSCODE_ARCH)\VSCode-win32-$(VSCODE_ARCH)-$(VSCODE_VERSION).zip"
          New-Item -ItemType Directory -Path .build\win32-$(VSCODE_ARCH) -Force
          exec { 7z.exe a -tzip $ArchivePath -x!CodeSignSummary*.md ..\VSCode-win32-$(VSCODE_ARCH)\* -r }
          echo "##vso[task.setvariable variable=CLIENT_PATH]$ArchivePath"
        condition: and(succeededOrFailed(), eq(variables['BUILT_CLIENT'], 'true'))
        displayName: Package client

      - powershell: |
          . build/azure-pipelines/win32/exec.ps1
          $ErrorActionPreference = "Stop"
          $ArchivePath = ".build\win32-$(VSCODE_ARCH)\vscode-server-win32-$(VSCODE_ARCH).zip"
          New-Item -ItemType Directory -Path .build\win32-$(VSCODE_ARCH) -Force
          exec { 7z.exe a -tzip $ArchivePath ..\vscode-server-win32-$(VSCODE_ARCH) -r }
          echo "##vso[task.setvariable variable=SERVER_PATH]$ArchivePath"
        condition: and(succeededOrFailed(), eq(variables['BUILT_SERVER'], 'true'))
        displayName: Package server

      - powershell: |
          . build/azure-pipelines/win32/exec.ps1
          $ErrorActionPreference = "Stop"
          $ArchivePath = ".build\win32-$(VSCODE_ARCH)\vscode-server-win32-$(VSCODE_ARCH)-web.zip"
          New-Item -ItemType Directory -Path .build\win32-$(VSCODE_ARCH) -Force
          exec { 7z.exe a -tzip $ArchivePath ..\vscode-server-win32-$(VSCODE_ARCH)-web -r }
          echo "##vso[task.setvariable variable=WEB_PATH]$ArchivePath"
        condition: and(succeededOrFailed(), eq(variables['BUILT_WEB'], 'true'))
        displayName: Package server (web)

      - powershell: |
          . build/azure-pipelines/win32/exec.ps1
          $ErrorActionPreference = "Stop"
          $env:ESRPPKI = "$(ESRP-PKI)"
          $env:ESRPAADUsername = "$(esrp-aad-username)"
          $env:ESRPAADPassword = "$(esrp-aad-password)"
          exec { yarn gulp "vscode-win32-$(VSCODE_ARCH)-system-setup" --sign }
          $SetupPath = ".build\win32-$(VSCODE_ARCH)\system-setup\VSCodeSetup-$(VSCODE_ARCH)-$(VSCODE_VERSION).exe"
          mv .build\win32-$(VSCODE_ARCH)\system-setup\VSCodeSetup.exe $SetupPath
          echo "##vso[task.setvariable variable=SYSTEM_SETUP_PATH]$SetupPath"
        displayName: Build system setup

      - powershell: |
          . build/azure-pipelines/win32/exec.ps1
          $ErrorActionPreference = "Stop"
          $env:ESRPPKI = "$(ESRP-PKI)"
          $env:ESRPAADUsername = "$(esrp-aad-username)"
          $env:ESRPAADPassword = "$(esrp-aad-password)"
          exec { yarn gulp "vscode-win32-$(VSCODE_ARCH)-user-setup" --sign }
          $SetupPath = ".build\win32-$(VSCODE_ARCH)\user-setup\VSCodeUserSetup-$(VSCODE_ARCH)-$(VSCODE_VERSION).exe"
          mv .build\win32-$(VSCODE_ARCH)\user-setup\VSCodeSetup.exe $SetupPath
          echo "##vso[task.setvariable variable=USER_SETUP_PATH]$SetupPath"
        displayName: Build user setup

      - powershell: echo "##vso[task.setvariable variable=ARTIFACT_PREFIX]attempt$(System.JobAttempt)_"
        condition: and(succeededOrFailed(), notIn(variables['Agent.JobStatus'], 'Succeeded', 'SucceededWithIssues'))
        displayName: Generate artifact prefix

      - task: 1ES.PublishPipelineArtifact@1
        inputs:
          targetPath: $(CLIENT_PATH)
          artifactName: $(ARTIFACT_PREFIX)vscode_client_win32_$(VSCODE_ARCH)_archive
          sbomBuildDropPath: $(Agent.BuildDirectory)/VSCode-win32-$(VSCODE_ARCH)
          sbomPackageName: "VS Code Windows $(VSCODE_ARCH)"
          sbomPackageVersion: $(Build.SourceVersion)
        condition: and(succeededOrFailed(), ne(variables['CLIENT_PATH'], ''))
        displayName: Publish archive

      - task: 1ES.PublishPipelineArtifact@1
        inputs:
          targetPath: $(SERVER_PATH)
          artifactName: $(ARTIFACT_PREFIX)vscode_server_win32_$(VSCODE_ARCH)_archive
          sbomBuildDropPath: $(Agent.BuildDirectory)/vscode-server-win32-$(VSCODE_ARCH)
          sbomPackageName: "VS Code Windows $(VSCODE_ARCH) Server"
          sbomPackageVersion: $(Build.SourceVersion)
        condition: and(succeededOrFailed(), ne(variables['SERVER_PATH'], ''), ne(variables['VSCODE_ARCH'], 'arm64'))
        displayName: Publish server archive

      - task: 1ES.PublishPipelineArtifact@1
        inputs:
          targetPath: $(WEB_PATH)
          artifactName: $(ARTIFACT_PREFIX)vscode_web_win32_$(VSCODE_ARCH)_archive
          sbomBuildDropPath: $(Agent.BuildDirectory)/vscode-server-win32-$(VSCODE_ARCH)-web
          sbomPackageName: "VS Code Windows $(VSCODE_ARCH) Web"
          sbomPackageVersion: $(Build.SourceVersion)
        condition: and(succeededOrFailed(), ne(variables['WEB_PATH'], ''), ne(variables['VSCODE_ARCH'], 'arm64'))
        displayName: Publish web server archive

      - task: 1ES.PublishPipelineArtifact@1
        inputs:
          targetPath: $(SYSTEM_SETUP_PATH)
          artifactName: $(ARTIFACT_PREFIX)vscode_client_win32_$(VSCODE_ARCH)_setup
          sbomBuildDropPath: $(Agent.BuildDirectory)/VSCode-win32-$(VSCODE_ARCH)
          sbomPackageName: "VS Code Windows $(VSCODE_ARCH) System Setup"
          sbomPackageVersion: $(Build.SourceVersion)
        condition: and(succeededOrFailed(), ne(variables['SYSTEM_SETUP_PATH'], ''))
        displayName: Publish system setup

      - task: 1ES.PublishPipelineArtifact@1
        inputs:
          targetPath: $(USER_SETUP_PATH)
          artifactName: $(ARTIFACT_PREFIX)vscode_client_win32_$(VSCODE_ARCH)_user-setup
          sbomBuildDropPath: $(Agent.BuildDirectory)/VSCode-win32-$(VSCODE_ARCH)
          sbomPackageName: "VS Code Windows $(VSCODE_ARCH) User Setup"
          sbomPackageVersion: $(Build.SourceVersion)
        condition: and(succeededOrFailed(), ne(variables['USER_SETUP_PATH'], ''))
        displayName: Publish user setup
